var inited=false;
$(document).ready(function () {
    console.log("loading stream");
    initOptions();
})

function initOptions() {
    //配置流量监测折线图
    var option = getStreamMonitorLineOption();
    var myChart = echarts.init(document.getElementById('streamMonitorChart'));
    myChart.setOption(option);
    //配置协议类型饼图
    option = getProtocolTypePieOption();
    myChart = echarts.init(document.getElementById('protocolTypeChart'));
    myChart.setOption(option);
    //配置响应码柱状图
    option = getResponseCodeBarOption();
    myChart = echarts.init(document.getElementById('responseCodeChart'));
    myChart.setOption(option);

    inited=true;

}

function loadWebStreamData() {
    if(!inited){
        setTimeout(loadWebStreamData,100);
        return;
    }
    //获取数据
    var data=isrealMode ? REAL_MODE_DATA : QUERY_MODE_DATA;
    //处理数据
    var webStreamData = parseStreamLineData(data.webStreamData.data, true, -1);
    //更新折线图数据
    var chart = echarts.getInstanceByDom(document.getElementById('streamMonitorChart'));
    var option={
        series: [{
            data: webStreamData.arrCurStream,
            markArea:{
                data:webStreamData.abnormalArea
            }
        }]
    };
    chart.setOption(option);
    //更新响应码柱状图
    let color = ['#003366', '#006699', '#4cabce', '#dd545d'];
    let index = 0;
    let xAxisData = [];
    result = [];
    var codeData=data.webStreamData.feature;
    for (const code in codeData) {
        xAxisData.push(code);
        result.push({
            value: codeData[code],
            itemStyle: {
                color: color[index++]
            }
        })
    }
    //应用数据
    var chart = echarts.getInstanceByDom(document.getElementById('responseCodeChart'));
    var option={
        xAxis: [{
            data: xAxisData
        }],
        series: [{
            data: result
        }]
    };
    chart.setOption(option);
    
}

function loadFirewallStreamData() {
    if(!inited){
        setTimeout(loadFirewallStreamData,100);
        return;
    }
    //获取数据
    var data=isrealMode ? REAL_MODE_DATA : QUERY_MODE_DATA;
    //更新流量折线图
    //处理数据
    var firewallStreamData = parseStreamLineData(data.firewallStreamData.data, true, -1);
    //应用数据
    var chart = echarts.getInstanceByDom(document.getElementById('streamMonitorChart'));
    var option={
        series: [{

        },{
            data: firewallStreamData.arrCurStream,
            markArea:{
                data:firewallStreamData.abnormalArea
            }
        }]
    };
    chart.setOption(option);
    //更新协议饼图
    var result = [];
    var protocolData=data.firewallStreamData.feature;
    for (const protocol in protocolData) {
        result.push({
            name: protocol,
            value: protocolData[protocol]
        })
    };
    var chart = echarts.getInstanceByDom(document.getElementById('protocolTypeChart'));
    var option={
        series: [{
            data: result
        }]
    };
    chart.setOption(option);

}

function loadAbnormalData() {
    if(!inited){
        setTimeout(loadAbnormalData,100);
        return;
    }
    //获取数据
    var data=isrealMode ? REAL_MODE_DATA : QUERY_MODE_DATA;
    //更新流量异常表格
    //处理数据
    var index=0;
    result = [];
    data.abnormalData.data.forEach((event) => {
        if(event.eventType=="streamAbnormal")
            result.push([++index, event.startTime, event.endTime]);
    });
    //应用数据
    $("#streamAlertTable").DataTable({
        data:result,
        columns: [
            { title: "序号" },
            { title: "开始时间" },
            { title: "结束时间" }
        ],
        paging:false,
        searching:false,
        info:false,
        ordering:false,
        destroy:true
    });

}

function loadAttackData() {

}